<template>
  <div class="com-container">
    <div class="com-chart" ref="city_ref">
    </div>
  </div>
</template>

<script>
import indexdata from '@/api/data/indexdata'

export default {
  props: ['timecity'],
  watch: {
    timecity: {
      async handler (newVal) {
        await this.timeCityChart()
      },
      deep: true
    }
  },
  data () {
    return {
      chartInstance: null,//图标实例
      allData: null,//图表数据
      size: null,//大小
      startValue: 0,//区域缩放的起点值
      endValue: 5,//区域缩放的终点值
      timerId: null,//定时器的id值
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    //增加屏幕大小变化监听的事件
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId)
  },
  methods: {
    async timeCityChart () {
      const timeCity = {}
      timeCity.startYear = this.timecity.startYear + '-01-01'
      timeCity.endYear = this.timecity.endYear + '-01-01'
      timeCity.city = this.timecity.city
      const res = await indexdata.getFigureCityByTime(timeCity)
      const figureCity = res.data.figureCity
      if(!figureCity || figureCity.length === 0){
        const option = {
          series:[{
            type:'bar',
            data:[]
          }]
        }
        this.allData = []
        this.chartInstance.setOption(option)
        return
      }
      this.allData = figureCity
      this.updateChart()
    },
    async initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.city_ref, 'dark')
      const initOption = {
        title: {
          text: '人物城市分布',
          left: 20,
          top: 20
        },
        grid: {
          top: '25%',
          left: '5%',
          right: '5%',
          bottom: '10%',
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          type: 'bar'
        }]
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData () {
      const res = await indexdata.getFigureCity()

      this.allData = res.data.figureCity
      this.updateChart()
    },
    updateChart () {
      //颜色数组
      const colorArr = [
        ['#77A977', '#FE895A'],
        ['#86BB88', '#7D96BC'],
        ['#409F02', '#B2A43C'],
      ]
      //所有省份名
      const CityArr = []
      this.allData.forEach(item => {
        CityArr.push(item.city)
      })
      //省份对应的销售额
      const valueArr = []
      this.allData.map(item => {
        valueArr.push(item.numbers)
      })
      // console.log(this.timecity)
      let titleText = '历史人物城市分布'
      if(typeof this.timecity !== 'undefined'){
        titleText = this.timecity.startYear + '年~'+ this.timecity.endYear+'年 历史人物城市分布'
      }
      const dataOption = {
        title:{
          text: titleText
        },
        xAxis: {
          data: CityArr
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {
                let targetColorArr = null
                if (arg.value > 300) {
                  targetColorArr = colorArr[0]
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1]
                } else {
                  targetColorArr = colorArr[2]
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ])
              }
            }
          }
        ]
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter () {
      this.size = (this.$refs.city_ref.offsetWidth / 100) * 3.6
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: this.size/1.2
          }
        },
        xAxis: {
          axisLabel: {
            fontSize: this.size / 1.5
          }
        },
        yAxis: {
          axisLabel: {
            fontSize: this.size / 1.5
          }
        },
        series: [
          {
            barWidth: this.size,
            itemStyle: {
              barBorderRadius: [this.size / 2, this.size / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adaptOption)
      this.chartInstance.resize()
    },
    startInterval () {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.startValue++
        this.endValue++

        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 5
        }
        this.updateChart()
      }, 2000)
    }
  },
}
</script>

<style scoped>

</style>
